﻿using AutoMapper;
using GGGK.Film.IService;
using GGGK.Film.Models;
using GGGK.Film.Models.Dto;
using Microsoft.AspNetCore.Mvc;

namespace GGGK.Film.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class MoviesController : ControllerBase
    {
        private readonly IMovieInfoService svc;
        private readonly IMapper mapper;

        public MoviesController(IMovieInfoService svc, IMapper mapper)
        {
            this.svc = svc;
            this.mapper = mapper;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="movieTitle"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        public PagedResult<MovieInfoDto> Get(string movieTitle = "", int pageSize = 10, int pageIndex = 1)
        {
            string sql = @"select m.*,t.DictName as 'MovieType',c.DictName as 'Color' from MovieInfo m
                        inner join VW_Dict t on m.MovieTypeId = t.DictValue and t.DictTypeName = '影片类型'
                        inner join VW_Dict c on m.MovieColor = c.DictValue and c.DictTypeName = '影片颜色'
                        where 1=1";

            if (!string.IsNullOrEmpty(movieTitle)) sql += " and MovieTitle like @movieTitle";

            string totleSql = $"select count(*) from ({sql}) ff";

            int total = svc.ExecuteScalar<int>(totleSql, new { movieTitle = $"%{movieTitle}%" });

            sql += @" order by CreateTime desc
                        offset @pageSize*(@pageIndex-1) rows fetch next @pageSize rows only";

            var data = svc.Query<MovieInfoDto>(sql, new { movieTitle = $"%{movieTitle}%", pageSize, pageIndex });

            return new PagedResult<MovieInfoDto>() { code = 1, msg = "成功", data = data, total = total };
        }
    }
}
